//
//  HHBaseViewController.swift
//  FBSnapshotTestCase
//
//  Created by renxibin on 2021/8/5.
//

import UIKit

 open class HHBaseViewController: UIViewController,UIGestureRecognizerDelegate,UINavigationControllerDelegate {

    //是否隐藏NavigationBar 写在viewDidLoad 在 viewWillAppear 中设置无效
    open var isHiddenNavigationBar : Bool = false
  
    
    /// 隐藏返回按钮
    open var isHiddenbackButton : Bool = false {
        didSet{
            
            backButton.isHidden = isHiddenbackButton
            
        }
        willSet{
           
        }
    }
    /// 返回按钮样式
    open var backButtonImageName : String = "nav_back" {
        didSet{
            
            setNavLeftBarButton(imageName: backButtonImageName)

        }
        willSet{
           
        }
    }
    
    /// 返回按钮设置
    open var backButton : UIButton!
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if isHiddenNavigationBar {
            self.navigationController?.delegate = self
        }
        //nav背景颜色
        self.navigationController?.navigationBar.barTintColor = UIColor.white
    }

    open override func viewDidLoad() {
        super.viewDidLoad()

        
        //默认设置
        defaultSet()
        //设置返回按钮
        setNavLeftBarButton(imageName: "nav_back")
        //设置右滑手势
        setRightBackPanGesture()

    }
    /// 默认设置
    func defaultSet() {
        //非透明
        self.navigationController?.navigationBar.isTranslucent = false
        //title颜色
        self.navigationController?.navigationBar.tintColor = UIColor.black
        //背景色
        self.navigationController?.navigationBar.barTintColor = UIColor.white
        //隐藏下划线
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    /// 自定义右滑返回手势
    func setRightBackPanGesture() {
        
        let taregt = self.navigationController?.interactivePopGestureRecognizer?.delegate
        let pan = UIPanGestureRecognizer.init(target: taregt, action: Selector(("handleNavigationTransition:")))
        pan.delegate = self
        self.view.addGestureRecognizer(pan)
        
    }
    /// 设置返回按钮
    func setNavLeftBarButton(imageName: String) {
        // 返回按钮
        backButton = UIButton(type: .custom)

        // 给按钮设置返回箭头图片
        backButton.setBackgroundImage(UIImage.init(named: imageName), for: .normal)
        //backButton.setImage(UIImage.init(named: "fanhui-2"), for: .normal)
        // 设置frame
        backButton.frame = CGRect(x: 200, y: 13, width: 18, height: 18)
        backButton.addTarget(self, action: #selector(backClick), for: .touchUpInside)

        // 自定义导航栏的UIBarButtonItem类型的按钮
        let backView = UIBarButtonItem(customView: backButton)

        // 重要方法，用来调整自定义返回view距离左边的距离
        let barButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        barButtonItem.width = -5

        // 返回按钮设置成功
        navigationItem.leftBarButtonItems = [barButtonItem, backView]
    }
    
    // MARK: ==========手势事件==============
    @objc public func backClick() {
        self.navigationController?.popViewController(animated: true)
    }
    @objc public func backItemPressed() {
        self.navigationController?.popViewController(animated: true)
    }
    @objc public func touchClick() {
        
    }
    
    // MARK: ======GestureDelegate====
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer) {
          //只有二级以及以下的页面允许手势返回
           return self.navigationController!.viewControllers.count > 1
       }
       return true
    }
    // 判断距离左边界小于25 触发
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let point = touch.location(in: self.view)
        return point.x < 25
    }
    
    // MARK:   右滑动画处理
    public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        if viewController == self {
            navigationController.setNavigationBarHidden(true, animated: true)
        }else{
            //系统相册继承自 UINavigationController 这个不能隐藏 所有就直接return
            if navigationController is UIImagePickerController {
                return;
            }
            
            //不在本页时，显示真正的navbar
            navigationController.setNavigationBarHidden(false, animated: true)
            //当不显示本页时，要么就push到下一页，要么就被pop了，那么就将delegate设置为nil，防止出现BAD ACCESS
            //之前将这段代码放在viewDidDisappear和dealloc中，这两种情况可能已经被pop了，self.navigationController为nil，这里采用手动持有navigationController的引用来解决
            if navigationController.delegate as! UIViewController == self {
                navigationController.delegate = nil;
            }
        }
    }
    
    
}

